package com.woniuxy.main.handler;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.main.pojo.Messages;
import com.woniuxy.main.service.MessagesService;
import com.woniuxy.main.util.SessionUtil;

import lombok.Data;

@Data
@Controller
@RequestMapping("/message")
@Scope("prototype")
public class MessagesHandler {
	@Resource
	private MessagesService messagesService;
	
	/**
	 * 添加聊天记录
	 * @param uid
	 * @param coachId
	 * @param content
	 * @return
	 */
	@RequestMapping("/saveChatRecord")
	@ResponseBody
	public String saveChatRecord(Integer currentUid,Integer talkerUid,String content){
		currentUid = (int) SessionUtil.getSession().getAttribute("uid");
		//System.out.println(studentId+":"+coachId+":"+content);
		if (content!=null ||content.length()!=0) {
			Messages messages = new Messages();
			messages.setUid(currentUid);
			messages.setTalker(talkerUid);
			messages.setContent(content);
			messages.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			messagesService.saveChatRecord(messages);
		}
		return "success";		
	}
	
	/**
	 * 获取历史记录
	 * @param uid
	 * @param coachId
	 * @return
	 */
	@RequestMapping("/getHistoryMessages")
	@ResponseBody
	public List<Messages> getHistoryMessages(Integer currentUid,Integer talkerUid) {
		currentUid = (int) SessionUtil.getSession().getAttribute("uid");
		
		Messages messages = new Messages();
		messages.setUid(currentUid);
		messages.setTalker(talkerUid);
		List<Messages> historyMessages = messagesService.getHistoryMessages(messages);
		
		return historyMessages;
	}
	
	/**
	 * 获取当前用户有关的会话--uid/talker
	 */
	@RequestMapping("/getAllMessages")
	@ResponseBody
	public List<Messages> getAllMessages(Messages messages){
		//当前用户id
		int currentUid = (int) SessionUtil.getSession().getAttribute("uid");
		messages.setUid(currentUid);
		//获取id
		int[] talker = messagesService.messagesAsUid(messages);
		int[] uid = messagesService.messagesAsTalker(messages);
		//合并去重
		Map<Integer, Integer> map = new HashMap<>();
		for (int t : talker) {
			map.put(t, currentUid);
		}
		for (int i : uid) {
			map.put(i, currentUid);
		}
		List<Messages> list = new ArrayList<>();
		//map转list
		Set<Entry<Integer, Integer>> entrySet = map.entrySet();
		Iterator<Entry<Integer, Integer>> iterator = entrySet.iterator();
		while (iterator.hasNext()){
			Integer talkerUid = iterator.next().getKey();		//获取聊天对象id
			
			String nickName = messagesService.getNickName(talkerUid);	//获取昵称
			
			Messages message = new Messages();
			message.setUid(currentUid);
			message.setTalker(talkerUid);
			
			List<Messages> l = messagesService.getHistoryMessages(message);
			message = l.get(l.size()-1);
			message.setNickName(nickName);
			message.setUid(talkerUid);
			message.setTalker(currentUid);
			
			list.add(message);
		}
		return list;
	}
	
}
